import axios from 'axios'
// eslint-disable-next-line no-unused-vars
import router from '@/router'
import qs from 'qs';
import { Notify } from 'vant';
import Cookie from 'js-cookie'
var Config_api = require('../../api.config');
console.log("请求url", process.env)
window.api_baseURL = "/renren-fast" //api地址


window.API = process.env.API_URL //api地址
window.NODE_ENV = process.env.NODE_ENV //环境
axios.defaults.timeout = 60 * 1000;
axios.defaults.baseURL = window.api_baseURL; //开发

console.log('axios.defaults.baseURL ', axios.defaults.baseURL)

window.hideLoading = () => {
    if (window.$loading) {
        window.$loading.close();
        window.$loading = null;
    }
};
// request全局拦截
axios.interceptors.request.use(config => {
    let reg_url = config.url
    console.log('reg_url', reg_url)
        //在 直播接口数组中 替换baseurl
    if (config.url.indexOf('/login') >= 0) {
        return config;
    } else {
        let token = Cookie.get("token_web");
        if (token) {
            config.headers.token = token;
        }
        return config;
    }
    return config
}, error => {
    //Loading hide
    window.hideLoading();
    return Promise.reject(error)
});
//response全局拦截
axios.interceptors.response.use(response => {
    const code = response.data.code;

    if (code === 0 || code === 1000 || code === 1001 || code === 60000) {
        return response
    } else if (code === 404) {
        return response
    } else if (code === 401 && window.location.pathname !== '/login' && window.location.pathname !== '/set-password') {
        Cookie.remove("token_web");
        Cookie.remove("sys_user_id");
        localStorage.removeItem("token_web");
        localStorage.removeItem("sys_user_id");
        localStorage.removeItem("BEFORE_QUESTION_LIST")
        let search = window.location.search ? window.location.search.substring(1).split('&') : []
        let query = {}
        if (search && search.length) {
            search.forEach(ele => {
                let item = ele.split('=')
                query[item[0]] = item[1]
            })
        }
        if (window.location.pathname == '/and-do-work' || window.location.pathname == '/examination-index-h5') {
            if (/android/gi.test(navigator.appVersion)) {
                console.log("token失效获取token");
                JsNiceInterface.getdoworkwebtoken();
            }
            if (!!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
                console.log("token失效获取IOStoken");
                window.webkit.messageHandlers.getdoworkwebtoken.postMessage({});
            }

        } else if (window.location.pathname == '/chat') {
            router.push({ path: '/login' })
        } else {
            router.push({ path: '/login', query: { path: window.location.pathname, query: JSON.stringify(query) } })
        }
        return response
    } else {
        if (code === 401) {
            Cookie.remove("token_web");
            Cookie.remove("sys_user_id");
            localStorage.removeItem("token_web");
            localStorage.removeItem("sys_user_id");
            localStorage.removeItem("BEFORE_QUESTION_LIST")
            return response
        } else {
            if (code) {
                Notify({
                    duration: 1500,
                    message: response.data.msg,
                    type: 'warning',
                });
            }

            return response
        }

        return response
    }
}, error => {
    if (error.response) {
        Notify({
            message: error.response || "系统异常",
            type: 'error',
            duration: 1500
        });
    }

    return Promise.reject(error.response)
});
let headers = {
    "Content-Type": "application/json;charset=UTF-8",
    "token": Cookie.get("token_web")
}
export const post = (url, params, config = {}) => {
    return axios({ method: 'post', url: url, data: params }).then((res) => {
        if (res.data.code === 0 || res.data.code === 1000 || res.data.code === 1001 || res.data.code === 60000) {
            return res.data
        } else {
            return null
        }
    })
}

export const get = (url, params, config = { headers }) => {
    return axios({
        method: 'get',
        url: url,
        params: params
    }).then((res) => {
        if (res.data.code === 0) {
            return res.data
        } else {
            return null
        }
    })
}

// 读取txt
export const getTxt = (url, params, config = { headers }) => {
        return axios({
            method: 'get',
            url: url,
            params: params
        }).then((res) => {
            return res
        })
    }
    // eslint-disable-next-line no-unused-vars
export const deletedata = (url, params, config = { headers }) => {
        return axios({
            method: 'delete',
            url: url,
            params: params
        }).then((res) => {
            if (res.data.code === 0) {
                return res.data.data
            } else {
                return {}
            }
        })
    }
    // eslint-disable-next-line no-unused-vars
export const putdata = (url, params, config = { headers }) => {
    return axios({
        method: 'put',
        url: url,
        params: params
    }).then((res) => {
        if (res.data.code === 0) {
            return res.data.data
        } else {
            return {}
        }
    })
}
export const baseURL = axios.defaults.baseURL;